home *** CD-ROM | disk | FTP | other *** search
/ Adobe Graphics & Publishing SDK 1996 December / Adobe Graphics & Publishing SDK 1996 December.iso / pc / ps40sdk / examples / import / gradientimport / common / gradientimportscripting.c < prev   
Encoding:
C/C++ Source or Header  |  1996-08-12  |  8.0 KB  |  309 lines

  1. /*
  2.     File: GradientImportScripting.c
  3.  
  4.     Copyright ⌐ 1990, Thomas Knoll.
  5.     Copyright ⌐ 1993-6, Adobe Systems Incorporated.
  6.     All rights reserved.
  7.  
  8.     C source file for scripting functions for import example.
  9. */
  10.  
  11. #include "GradientImport.h"
  12.  
  13. /*****************************************************************************/
  14. /* Opens up the scripting parameters to begin reading commands. */
  15.  
  16. void OpenScriptParams (GPtr globals)
  17. {
  18.     PIDescriptorHandle            subHandle = NULL;
  19.     DescriptorKeyID                key = 0;
  20.     DescriptorTypeID            type = 0;
  21.     int16                        loop = 0;
  22.     int32                        flags = 0;
  23.     OSErr                        gotErr = noErr, stickyError = noErr;
  24.     Boolean                        invert = false, returnValue = true, leaveEarly = false;
  25.     
  26.     if (DescriptorAvailable())
  27.     { 
  28.         gToken = OpenReader(NULL); 
  29.         /* don't know how many we're going to be passed, so don't bother with array */
  30.         if (gToken)
  31.         {
  32.             while (!leaveEarly)
  33.             {
  34.                 leaveEarly = PIGetKey(gToken, &key, &type, &flags);
  35.                 switch (key)
  36.                 {
  37.                     case keyMultiImportCount:
  38.                         PIGetCount(gToken, &(gCount));
  39.                         leaveEarly = true;
  40.                         break;
  41.                     /* We're going to ignore all other keys.  What we're looking for is our
  42.                       list, which will be preceded by our count key. Once we find it, we'll drop
  43.                        out, to eventually call our Read routine. */
  44.                 }
  45.             }
  46.         }
  47.     gQueryForParameters = PlayDialog();
  48.     }
  49.     /* return true if want to show our Dialog */
  50. }
  51.         
  52. /*****************************************************************************/
  53.  
  54. /* Reads the next batch of values and sets the global parameters. */
  55.  
  56. void ReadScriptParams (GPtr globals)
  57. {
  58.     int16                        loop = 0;
  59.     int32                        flags = 0;
  60.     DescriptorTypeID            type = 0;
  61.     DescriptorTypeID            passType = classMultiImportStruct;
  62.     DescriptorKeyID                key = 0;
  63.     DescriptorKeyIDArray        subKeyIDArray = 
  64.                                 { keyRows, keyColumns, 
  65.                                   keyOurMode, 0 }; /* keyInvert */
  66.     PIDescriptorHandle            subHandle = NULL;
  67.     PIReadDescriptor            subToken = NULL;
  68.     OSErr                        stickyError = noErr;
  69.     
  70.     if (DescriptorAvailable())
  71.     {
  72.         if (gToken)
  73.         {
  74.             if (gCount > 0)
  75.             {
  76.                 gLastInvert = false; // if we've got a valid descriptor, assume NO invert
  77.                 PIGetObj(gToken, &passType, &subHandle);
  78.                 subToken = OpenReadDesc(subHandle, subKeyIDArray);
  79.                 if (subToken)
  80.                 {
  81.                     SwitchScriptInfo (globals, subToken);
  82.                     stickyError = CloseReadDesc(subToken); // done.
  83.                     subToken = NULL; // just in case
  84.                     PIDisposeHandle(subHandle); // dispose.
  85.                     subHandle = NULL; // just in case.
  86.         
  87.                     if (stickyError)
  88.                     {
  89.                         if (stickyError == -1715) // missedParamErr
  90.                             ; /* missing parameter.  Walk keyIDArray to find which one. */
  91.                         else
  92.                             gResult = stickyError; // real error occured
  93.                     }
  94.                     gContinueImport = true; // we got something, so keep going!
  95.                 }
  96.                 gCount--;
  97.             }
  98.             if (gCount < 1)                        
  99.                 CloseScriptParams(globals); // close em up!
  100.         } /* no readToken */
  101.     } /* not correct descriptorParameters version */
  102. }
  103.  
  104. /*****************************************************************************/
  105.  
  106. /* Checks the parameters against scripting-returned parameters, if any, and
  107.    updates our parameters to match ones given to us by the scripting system. */
  108.  
  109. void CloseScriptParams (GPtr globals)
  110. {
  111.     OSErr            stickyError = noErr;
  112.  
  113.     if (DescriptorAvailable())
  114.     {    
  115.         if (gToken)
  116.         {
  117.             stickyError = CloseReader(&gToken); // done.
  118.  
  119.             if (stickyError)
  120.             {
  121.                 if (stickyError == errMissingParameter)
  122.                     ; /* missing parameter.  Check our parameters and coerce if need be. */
  123.                 else
  124.                     gResult = stickyError; // real error occured
  125.             }
  126.         }
  127.     }
  128.     gCount = 0; /* reset this puppy */
  129.     gContinueImport = false;
  130. }
  131.  
  132.         
  133. /*****************************************************************************/
  134. /* Updates globals from scripting parameters                                 */
  135.  
  136. void SwitchScriptInfo (GPtr globals, PIReadDescriptor token)
  137. {
  138.     double                        x;
  139.     OSType                        mode = ourRGBColorMode;
  140.     const double                minRows = kRowsMin, maxRows = kRowsMax,
  141.                                 minColumns = kColumnsMin, maxColumns = kColumnsMax;
  142.     unsigned long                pixelsUnitPass = unitPixels;
  143.     DescriptorKeyID                key = 0;
  144.     DescriptorTypeID            type = 0;
  145.     int16                        loop = 0;
  146.     int32                        flags = 0;
  147.     int32                        count = 0;
  148.     Boolean                        invert = false;
  149.     
  150.     while (PIGetKey(token, &key, &type, &flags))
  151.     {
  152.         switch (key)
  153.         {
  154.             case keyRows:
  155.                 PIGetPinUnitFloat(token, &minRows, &maxRows, &pixelsUnitPass, &x);
  156.                 gLastRows = (short)x;
  157.                 break;
  158.             case keyColumns:
  159.                 PIGetPinUnitFloat(token, &minColumns, &maxColumns, &pixelsUnitPass, &x);
  160.                 gLastCols = (short)x;
  161.                 break;
  162.             case keyOurMode:
  163.                 PIGetEnum(token, &mode);
  164.                 gLastMode = GetPlugInMode(mode);
  165.                 break;
  166.             case keyInvert:
  167.                 PIGetBool(token, &invert);
  168.                 gLastInvert = invert;
  169.                 break;
  170.         }
  171.     }
  172. }
  173.  
  174. /*****************************************************************************/
  175.  
  176. /* Checks for any data then writes our parameters to the scripting system. */
  177.  
  178. OSErr CheckAndWriteScriptParams (GPtr globals)
  179. {
  180.     OSErr    gotErr = noErr;
  181.     
  182.     if (gLastImages) gotErr = WriteScriptParams(globals);
  183.     else gResult = userCanceledErr; // standard return from multiple acquire
  184.     
  185.     return gotErr;
  186. }
  187.  
  188. /*****************************************************************************/
  189.  
  190. /* Writes our parameters to the scripting system. */
  191.  
  192. OSErr WriteScriptParams (GPtr globals)
  193. {
  194.     OSType                        mode = GetGradientMode(gLastMode);
  195.     const double                rows = gLastRows, columns = gLastCols;
  196.     Boolean                        invert = gLastInvert;
  197.     uint32                        count = gLastImages;
  198.     PIWriteDescriptor            token = NULL;
  199.     OSErr                        stickyError = noErr;
  200.             
  201.     if (DescriptorAvailable())
  202.     {
  203.         token = OpenWriter();
  204.         if (token)
  205.         {
  206.             PIPutCount(token, keyMultiImportCount, count);
  207.             for (count = 0; count < (uint32)gLastImages; count++)
  208.             {
  209.                 PIPutObj(token, keyMultiImportInfo, classMultiImportStruct, &gArray [count]);
  210.                 /* routine disposes handle when its done with it */
  211.             }
  212.             
  213.             gLastImages = 0; // reset
  214.             stickyError = CloseWriter(&token); // returns descriptor and sets plugInDialogOptional
  215.         }
  216.         
  217.     }
  218.     return stickyError;
  219. }
  220.  
  221. /*****************************************************************************/
  222. /* Returns a descriptor from our parameters                                  */
  223.  
  224. void CreateDescriptor (GPtr globals)
  225. {
  226.     OSType                        mode = GetGradientMode(gLastMode);
  227.     const double                rows = gLastRows, columns = gLastCols;
  228.     Boolean                        invert = gLastInvert;
  229.     PIWriteDescriptor            token = NULL;
  230.     PIDescriptorHandle            h;
  231.     OSErr                        stickyError = noErr;
  232.             
  233.     if (DescriptorAvailable())
  234.     {
  235.         token = OpenWriter();
  236.         if (token)
  237.         {
  238.             PIPutUnitFloat(token, keyRows, unitPixels, &rows);
  239.             PIPutUnitFloat(token, keyColumns, unitPixels, &columns);
  240.             PIPutEnum(token, keyOurMode, typeGradientMode, mode);
  241.             if (invert) PIPutBool(token, keyInvert, invert);
  242.             
  243.             stickyError = CloseWriteDesc(token, &h);
  244.             token = NULL;
  245.                     
  246.             if (!stickyError)
  247.             {
  248.                 if (gLastImages >= kMaxDescriptors)
  249.                     gLastImages--; // just keep replacing last one
  250.                             
  251.                 gArray [gLastImages++] = h;
  252.                         
  253.                 gArray [gLastImages] = h = NULL; // just in case
  254.                 /* don't forget to dispose of this after storing with PutObject! */
  255.             }
  256.         }
  257.     }
  258. }
  259.  
  260. /*****************************************************************************/
  261. /* Takes the plug-in mode and returns the GradientMode                         */
  262.  
  263. OSType GetGradientMode (short plugInMode)
  264.  
  265. {
  266.     switch (plugInMode)
  267.     {
  268.         case plugInModeBitmap:
  269.             return ourBitmapMode;
  270.             break;
  271.         case plugInModeGrayScale:
  272.             return ourGrayscaleMode;
  273.             break;
  274.         case plugInModeIndexedColor:
  275.             return ourIndexedColorMode;
  276.             break;
  277.         case plugInModeRGBColor:
  278.         default:
  279.             return ourRGBColorMode;
  280.             break;
  281.     }
  282. }
  283.  
  284. /*****************************************************************************/
  285. /* Takes the GradientMode and returns the plug-in mode                           */
  286.  
  287. short GetPlugInMode (OSType gradientMode)
  288.  
  289. {
  290.     switch (gradientMode)
  291.     {
  292.         case ourBitmapMode:
  293.             return plugInModeBitmap;
  294.             break;
  295.         case ourGrayscaleMode:
  296.             return plugInModeGrayScale;
  297.             break;
  298.         case ourIndexedColorMode:
  299.             return plugInModeIndexedColor;
  300.             break;
  301.         case ourRGBColorMode:
  302.         default:
  303.             return plugInModeRGBColor;
  304.             break;
  305.     }
  306. }
  307.  
  308. /*****************************************************************************/
  309.